/*
 * Copyright (c) 2011 Auzingers
 * Created by Oui-IT
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.auzingers.docsorganizer.gui;

import com.auzingers.docsorganizer.R;
import com.auzingers.docsorganizer.entity.GoogleCollection;
import com.auzingers.docsorganizer.entity.GoogleDocument;
import com.auzingers.docsorganizer.entity.GoogleEntry;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Collections;
import java.util.List;

/**
 * Adapter for list view displaying Google entries.
 */
class GoogleEntryListAdapter extends BaseAdapter {

    private Context ctx;

    private List<GoogleEntry> data = Collections.emptyList();

    private Drawable imgFolder;

    private Drawable imgDocument;

    private Drawable imgSpreadsheet;

    private Drawable imgPresentation;

    private Drawable imgDrawing;

    private Drawable imgForm;

    private Drawable imgFusion;

    private Drawable imgUnknown;

    private Drawable imgSharedFolder;

    private Drawable imgSharedDocument;

    private Drawable imgSharedSpreadsheet;

    private Drawable imgSharedPresentation;

    private Drawable imgSharedDrawing;

    private Drawable imgSharedForm;

    private Drawable imgSharedFusion;

    private Drawable imgSharedUnknown;

    /**
     * Creates a new list adapter for Google entries.
     * @param ctx current context
     * @param entries entries to show in list
     */
    public GoogleEntryListAdapter(Context ctx, List<GoogleEntry> entries) {
        data = entries;
        this.ctx = ctx;
        final Resources res = ctx.getResources();
        imgFolder = res.getDrawable(R.drawable.dt_folder);
        imgDocument = res.getDrawable(R.drawable.dt_document);
        imgSpreadsheet = res.getDrawable(R.drawable.dt_spreadsheet);
        imgPresentation = res.getDrawable(R.drawable.dt_presentation);
        imgDrawing = res.getDrawable(R.drawable.dt_drawing);
        imgForm = res.getDrawable(R.drawable.dt_form);
        imgFusion = res.getDrawable(R.drawable.dt_fusion);
        imgUnknown = res.getDrawable(R.drawable.dt_unknowntype);

        imgSharedFolder = res.getDrawable(R.drawable.dt_shared_folder);
        imgSharedDocument = res.getDrawable(R.drawable.dt_shared_document);
        imgSharedSpreadsheet = res.getDrawable(R.drawable.dt_shared_spreadsheet);
        imgSharedPresentation = res.getDrawable(R.drawable.dt_shared_presentation);
        imgSharedDrawing = res.getDrawable(R.drawable.dt_shared_drawing);
        imgSharedForm = res.getDrawable(R.drawable.dt_shared_form);
        imgSharedFusion = res.getDrawable(R.drawable.dt_shared_fusion);
        imgSharedUnknown = res.getDrawable(R.drawable.dt_shared_unknowntype);
    }

    public int getCount() {
        return data.size();
    }

    /** {@inheritDoc} */
    public Object getItem(int pos) {
        return data.get(pos);
    }

    /** {@inheritDoc} */
    public long getItemId(int pos) {
        return pos;
    }

    /** {@inheritDoc} */
    public View getView(int pos, View convertView, ViewGroup grp) {
        View v = convertView;
        if (v == null) {
            final LayoutInflater vi = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row_entry, null);
        }
        final GoogleEntry entry = data.get(pos);
        if (entry != null) {
            final TextView title = (TextView) v.findViewById(R.id.re_title);
            if (title != null) {
                title.setText(entry.getTitle());
            }
            final ImageView img = (ImageView) v.findViewById(R.id.re_image);
            if (img != null) {
                img.setImageDrawable(getImageDrawable(entry));
            }
        }
        return v;
    }

    /**
     * Returns the drawable for the given GoogleEntry, i.e. a folder or a document icon or a spreadsheet icon etc.
     * @param entry entry for which an icon is requested
     * @return the icon, if available, or null
     */
    private Drawable getImageDrawable(GoogleEntry entry) {
        final Drawable result;
        final boolean shared = entry.isSharedEntry();
        if (entry instanceof GoogleDocument) {
            final String kind = entry.getCategoryKind();
            if ("document".equals(kind)) {
                result = shared ? imgSharedDocument : imgDocument;
            } else if ("spreadsheet".equals(kind)) {
                result = shared ? imgSharedSpreadsheet : imgSpreadsheet;
            } else if ("form".equals(kind)) {
                result = shared ? imgSharedForm : imgForm;
            } else if ("presentation".equals(kind)) {
                result = shared ? imgSharedPresentation : imgPresentation;
            } else if ("drawing".equals(kind)) {
                result = shared ? imgSharedDrawing : imgDrawing;
            } else if ("table".equals(kind)) {
                result = shared ? imgSharedFusion : imgFusion;
            } else {
                result = shared ? imgSharedUnknown : imgUnknown;
            }
        } else if (entry instanceof GoogleCollection) {
            result = shared ? imgSharedFolder : imgFolder;
        } else {
            result = null;
        }
        return result;
    }
}
